   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/**
 *
 * @author MAN
 */
public class Cliente_DAO extends Model implements DaoGenerico {
    
    
    public boolean existe(Object o) throws SQLException{
             Integer id =(Integer)o;
             boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=getCon().prepareStatement("SELECT COUNT(*) FROM cliente WHERE codigo_cliente =?");
			pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}
                 System.out.print("Valor sw:"+sw);
		return sw;
	}



      public Object load(ResultSet rs) throws SQLException {

        Cliente c = new Cliente();
        c.setCodigo(rs.getInt(1));
        c.setNombre(rs.getString(2));
  
        c.setTelefono(rs.getString(3));
        c.setEmail(rs.getString(4));
        c.setDireccion(rs.getString(5));
     
        return c;
    }
      public Object extrae (Object  o) throws SQLException{
            Integer id =(Integer)o;
     
                    Connection conex=getCon();

                if (!isConnect())
			throw new SQLException("no hay conexcion");
                Cliente c= new Cliente();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM cliente WHERE codigo_cliente=?");
			pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){
			  c=(Cliente)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return c;
     }
      

        public void crear(Object o)throws SQLException{
         Cliente c=(Cliente)o;
             Connection conex=getCon();
                  Integer id= new Integer(c.getCodigo());
                if(existe(id))
                    throw new SQLException("Ya existe esta cliente");
		PreparedStatement pstmt=null;

		try{

			pstmt=conex.prepareStatement("insert into cliente values (?,?,?,?,?)");

			pstmt.setInt(1,c.getCodigo());
			pstmt.setString(2,c.getNombre());
		        pstmt.setString(5,c.getTelefono());
			pstmt.setString(6,c.getEmail());
                        pstmt.setString(7,c.getDireccion());
		

			pstmt.executeUpdate();
                    
		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}
     
  }



       public List listar() throws SQLException {
              Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Cliente c= null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from cliente ");
            rs = ps.executeQuery();

            while (rs.next()) {
                c=(Cliente) load(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public  boolean update (Cliente c) throws SQLException {
              Connection conex=getCon();
                                Integer id= new Integer(c.getCodigo());

       if (!existe(id)) {
           throw new SQLException ("Cliente no registrado "+c.getCodigo());
       }
       PreparedStatement pstmt = null;
       boolean res = false;
       try {
         pstmt = conex.prepareStatement("Update cliente set codigo_cliente  = ?, nombre = ?, telefono  = ?, email = ?,direccion  = ?  where codigo_cliente = ?");
        	pstmt.setInt(1,c.getCodigo());
			pstmt.setString(2,c.getNombre());
		        pstmt.setString(5,c.getTelefono());
			pstmt.setString(6,c.getEmail());
                        pstmt.setString(7,c.getDireccion());

        

         int r = pstmt.executeUpdate();
       if (r != 0) {
          res = true;
       }
       } finally {
          if (pstmt != null) {
            pstmt.close();
          }
       }
         return res;
 }

    public Object extrae2(Object id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listarlista(List l) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
